package pl.jtb.ctl.tabory.dao.impl.mybatis.osgi;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import pl.jtb.ctl.tabory.dao.api.services.ICompositionService;
import pl.jtb.ctl.tabory.dao.api.services.IVagonService;
import pl.jtb.ctl.tabory.dao.data.vo.CompositionVO;
import pl.jtb.ctl.tabory.dao.data.vo.VagonVO;
import pl.jtb.ctl.tabory.dao.impl.mybatis.servicemanager.ServiceManager;

public class Activator implements BundleActivator {

    private final static Logger logger = Logger.getLogger(Activator.class);

    public void start(BundleContext context) throws Exception {
        logger.info("Starting bundle: " + context.getBundle().getSymbolicName());
        // tearDown();
        // test();
        // tearDown();
    }

    public void stop(BundleContext context) throws Exception {
        logger.info("Stopping bundle: " + context.getBundle().getSymbolicName());
    }

    protected void test() throws Exception {
        ICompositionService compositionService = ServiceManager.getInstance().getCompositionService();
        IVagonService vagonService = ServiceManager.getInstance().getVagonService();

        VagonVO vagon1 = new VagonVO();
        vagon1.setActive(true);
        vagon1.setDistance(123.3);
        vagon1.setInternalId("vagon1");
        vagon1.setRevisionCorrection(123.1);
        vagon1.setRevisionDistance(234.4);
        vagon1.setRevOneFrom(new Date());
        vagon1.setRevOneUntil(new Date());
        vagon1.setRevTwoFrom(new Date());
        vagon1.setRevTwoUntil(new Date());

        VagonVO vagon2 = new VagonVO();
        vagon2.setActive(true);
        vagon2.setDistance(123.3);
        vagon2.setInternalId("vagon2");
        vagon2.setRevisionCorrection(123.1);
        vagon2.setRevisionDistance(234.4);
        vagon2.setRevOneFrom(new Date());
        vagon2.setRevOneUntil(new Date());
        vagon2.setRevTwoFrom(new Date());
        vagon2.setRevTwoUntil(new Date());

        CompositionVO composition = new CompositionVO();
        composition.setActive(true);
        composition.setInternalId("comp1");

        compositionService.saveUpdateComposition(composition);
        vagonService.saveUpdateVagon(vagon1);
        vagonService.saveUpdateVagon(vagon2);

        Map<CompositionVO, List<VagonVO>> mapRelations = new HashMap<CompositionVO, List<VagonVO>>();
        List<VagonVO> listVagons = new ArrayList<VagonVO>();
        listVagons.add(vagon1);
        listVagons.add(vagon2);
        mapRelations.put(composition, listVagons);

        compositionService.saveUpdateCompositionVagonRelation(mapRelations);
    }

    protected void tearDown() throws Exception {
        ICompositionService compositionService = ServiceManager.getInstance().getCompositionService();
        IVagonService vagonService = ServiceManager.getInstance().getVagonService();
        List<CompositionVO> listAllCompositions = compositionService.getListAllCompositions();
        List<VagonVO> listAllVagons = vagonService.getListAllVagons();
        for (VagonVO vagonVO : listAllVagons) {
            vagonService.deleteVagon(vagonVO);
        }
        for (CompositionVO compositionVO : listAllCompositions) {
            compositionService.deleteComposition(compositionVO);
        }
    }

}
